<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="职位ID" prop="positionId">
        <el-input
          v-model="queryParams.positionId"
          placeholder="请输入职位ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司ID" prop="companyId">
        <el-input
          v-model="queryParams.companyId"
          placeholder="请输入公司ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职位名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入职位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职位类型" prop="type">
        <el-input
          v-model="queryParams.type"
          placeholder="请输入职位类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工作地点" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入工作地点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实习时长" prop="duration">
        <el-input
          v-model="queryParams.duration"
          placeholder="请输入实习时长"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="薪资待遇" prop="salary">
        <el-input
          v-model="queryParams.salary"
          placeholder="请输入薪资待遇"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="核心职责" prop="duties">
        <el-input
          v-model="queryParams.duties"
          placeholder="请输入核心职责"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任职要求" prop="requirements">
        <el-input
          v-model="queryParams.requirements"
          placeholder="请输入任职要求"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="转正率" prop="conversionRate">
        <el-input
          v-model="queryParams.conversionRate"
          placeholder="请输入转正率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="晋升路径" prop="promotionPath">
        <el-input
          v-model="queryParams.promotionPath"
          placeholder="请输入晋升路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="删除标志" prop="delFlag">
        <el-input
          v-model="queryParams.delFlag"
          placeholder="请输入删除标志"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['internship:position:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['internship:position:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['internship:position:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['internship:position:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="positionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="职位ID" align="center" prop="positionId" /> -->
      <!-- <el-table-column label="公司ID" align="center" prop="companyId" /> -->
      <el-table-column label="职位名称" align="center" prop="title" />
      <el-table-column label="职位类型" align="center" prop="type" />
      <el-table-column label="工作地点" align="center" prop="location" />
      <el-table-column label="实习时长" align="center" prop="duration" />
      <el-table-column label="薪资待遇" align="center" prop="salary" />
      <el-table-column label="核心职责" align="center" prop="duties" show-overflow-tooltip/>
      <el-table-column label="任职要求" align="center" prop="requirements" show-overflow-tooltip/>
      <el-table-column label="转正率" align="center" prop="conversionRate" />
      <el-table-column label="晋升路径" align="center" prop="promotionPath" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="投递所需积分" align="center" prop="delFlag" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['internship:position:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['internship:position:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改职位信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公司ID" prop="companyId">
          <el-input v-model="form.companyId" placeholder="请输入公司ID" />
        </el-form-item>
        <el-form-item label="职位名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入职位名称" />
        </el-form-item>
        <el-form-item label="职位类型" prop="type">
          <el-input v-model="form.type" placeholder="请输入职位类型" />
        </el-form-item>
        <el-form-item label="工作地点" prop="location">
          <el-input v-model="form.location" placeholder="请输入工作地点" />
        </el-form-item>
        <el-form-item label="实习时长" prop="duration">
          <el-input v-model="form.duration" placeholder="请输入实习时长" />
        </el-form-item>
        <el-form-item label="薪资待遇" prop="salary">
          <el-input v-model="form.salary" placeholder="请输入薪资待遇" />
        </el-form-item>
        <el-form-item label="核心职责" prop="duties">
          <el-input   type="textarea"
          :rows="5" v-model="form.duties" placeholder="用句号做换行圆点区分" />
        </el-form-item>
        <el-form-item label="任职要求" prop="requirements">
          <el-input  type="textarea"
          :rows="5" v-model="form.requirements" placeholder="用句号做换行圆点区分" />
        </el-form-item>
        <el-form-item label="转正率" prop="conversionRate">
          <el-input v-model="form.conversionRate" placeholder="请输入转正率" />
        </el-form-item>
        <el-form-item label="晋升路径" prop="promotionPath">
          <el-input v-model="form.promotionPath" placeholder="请输入晋升路径" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-divider content-position="center">职位申请记录信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddBolePostionApplication">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteBolePostionApplication">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="bolePostionApplicationList" :row-class-name="rowBolePostionApplicationIndex" @selection-change="handleBolePostionApplicationSelectionChange" ref="bolePostionApplication">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="申请人ID" prop="userId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.userId" placeholder="请输入申请人ID" />
            </template>
          </el-table-column>
          <el-table-column label="申请时间" prop="applyTime" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.applyTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择申请时间" />
            </template>
          </el-table-column>
          <el-table-column label="申请状态" prop="status" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.status" placeholder="请选择申请状态">
                <el-option
                  v-for="dict in dict.type.bole_event_registration"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPosition, getPosition, delPosition, addPosition, updatePosition } from "@/api/internship/position";

export default {
  name: "Position",
  dicts: ['bole_event_registration'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedBolePostionApplication: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 职位信息表格数据
      positionList: [],
      // 职位申请记录表格数据
      bolePostionApplicationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        positionId: null,
        companyId: null,
        title: null,
        type: null,
        location: null,
        duration: null,
        salary: null,
        duties: null,
        requirements: null,
        conversionRate: null,
        promotionPath: null,
        createTime: null,
        delFlag: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyId: [
          { required: true, message: "公司ID不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "职位名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询职位信息列表 */
    getList() {
      this.loading = true;
      listPosition(this.queryParams).then(response => {
        this.positionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        positionId: null,
        companyId: null,
        title: null,
        type: null,
        location: null,
        duration: null,
        salary: null,
        duties: null,
        requirements: null,
        conversionRate: null,
        promotionPath: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.bolePostionApplicationList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.positionId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加职位信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const positionId = row.positionId || this.ids
      getPosition(positionId).then(response => {
        this.form = response.data;
        this.bolePostionApplicationList = response.data.bolePostionApplicationList;
        this.open = true;
        this.title = "修改职位信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.bolePostionApplicationList = this.bolePostionApplicationList;
          if (this.form.positionId != null) {
            updatePosition(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPosition(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const positionIds = row.positionId || this.ids;
      this.$modal.confirm('是否确认删除职位信息编号为"' + positionIds + '"的数据项？').then(function() {
        return delPosition(positionIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 职位申请记录序号 */
    rowBolePostionApplicationIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 职位申请记录添加按钮操作 */
    handleAddBolePostionApplication() {
      let obj = {};
      obj.userId = "";
      obj.applyTime = "";
      obj.status = "";
      this.bolePostionApplicationList.push(obj);
    },
    /** 职位申请记录删除按钮操作 */
    handleDeleteBolePostionApplication() {
      if (this.checkedBolePostionApplication.length == 0) {
        this.$modal.msgError("请先选择要删除的职位申请记录数据");
      } else {
        const bolePostionApplicationList = this.bolePostionApplicationList;
        const checkedBolePostionApplication = this.checkedBolePostionApplication;
        this.bolePostionApplicationList = bolePostionApplicationList.filter(function(item) {
          return checkedBolePostionApplication.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleBolePostionApplicationSelectionChange(selection) {
      this.checkedBolePostionApplication = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('internship/position/export', {
        ...this.queryParams
      }, `position_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
